package org.apache.jmeter.gui.rcp.views;

import java.util.Iterator;

import org.apache.jmeter.gui.rcp.tree.JMeterTreeNode;
import org.apache.jmeter.gui.rcp.tree.JMeterTreeViewContentProvider;
import org.apache.jmeter.gui.rcp.tree.JMeterViewLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.internal.Model;
import org.eclipse.ui.part.ViewPart;

public class JMeterNavigationView extends ViewPart {
	public static final String ID = "org.apache.jmeter.gui.rcp.views.jmeterNavigationView";
	private TreeViewer viewer;
	private JMeterTreeViewContentProvider	contentProvider = null;
	private JMeterViewLabelProvider	labelProvider = null;
	
	public JMeterNavigationView() {
		// TODO Auto-generated constructor stub
	}

	public void createPartControl(Composite parent) {
		contentProvider = new JMeterTreeViewContentProvider();
		labelProvider = new JMeterViewLabelProvider();
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		viewer.setContentProvider(contentProvider);
		viewer.setLabelProvider(labelProvider);
		viewer.setInput(createDummyModel());
		
		hookListeners();
	}

	protected void hookListeners() {
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				// if the selection is empty clear the label
				if(event.getSelection().isEmpty()) {
					System.out.println("-----> Selection:  IS EMPTY!");
					return;
				}
				if(event.getSelection() instanceof IStructuredSelection) {
					IStructuredSelection selection = (IStructuredSelection)event.getSelection();
					
					for (Iterator iterator = selection.iterator(); iterator.hasNext();) {
						Object domain = (JMeterTreeNode) iterator.next();
						String value = labelProvider.getText(domain);
						System.out.println("-----> Selection:  " + value);
					}
				}
			}
		});
	}

	public void setFocus() {
		viewer.getControl().setFocus();

	}

    /**
     * We will set up a dummy model to initialize tree heararchy. In real
     * code, you will connect to a real model and expose its hierarchy.
     */
    private JMeterTreeNode createDummyModel() {
    	JMeterTreeNode root = null;
    	JMeterTreeNode parent = null;
    	JMeterTreeNode child1 = null;
    	JMeterTreeNode child2 = null;
    	JMeterTreeNode child3 = null;
    	JMeterTreeNode child4 = null;
    	
    	root = new JMeterTreeNode("");
      
    	parent = new JMeterTreeNode("Test Plan", "org.apache.jmeter.control.gui.rcp.TestPlanGui");
    	child1 = new JMeterTreeNode("Thread Group", "org.apache.jmeter.threads.gui.rcp.ThreadGroupGui");
    	parent.addChild(child1);
    	child2 = new JMeterTreeNode("Interleave Controller", "org.apache.jmeter.control.gui.rcp.AbstractControllerGui");
    	child1.addChild(child2);
    	child3 = new JMeterTreeNode("If Controller", "org.apache.jmeter.control.gui.rcp.AbstractControllerGui");
    	child2.addChild(child3);
    	child4 = new JMeterTreeNode("JDBC Request", "org.apache.jmeter.samplers.gui.rcp.AbstractSamplerGui");
    	child3.addChild(child4);
    	child4 = new JMeterTreeNode("HTTP Request", "org.apache.jmeter.samplers.gui.rcp.AbstractSamplerGui");
    	child3.addChild(child4);
    	child3 = new JMeterTreeNode("ForEach Controller", "org.apache.jmeter.control.gui.rcp.AbstractControllerGui");
    	child2.addChild(child3);
    	child4 = new JMeterTreeNode("FTP Request", "org.apache.jmeter.samplers.gui.rcp.AbstractSamplerGui");
    	child3.addChild(child4);
    	child4 = new JMeterTreeNode("Aggregate Request", "org.apache.jmeter.samplers.gui.rcp.AbstractSamplerGui");
    	child3.addChild(child4);
    	child2 = new JMeterTreeNode("Simple Controller", "org.apache.jmeter.control.gui.rcp.AbstractControllerGui");
    	child1.addChild(child2);
    	child3 = new JMeterTreeNode("Counter", "org.apache.jmeter.samplers.gui.rcp.AbstractSamplerGui");
    	child2.addChild(child3);
    	root.addChild(parent);
    	
    	parent = new JMeterTreeNode("WorkBench", "org.apache.jmeter.report.gui.rcp.ReportPageGui");
    	root.addChild(parent);
    	
//    	JMeterTreeNode to3 = new JMeterTreeNode("Sent");
//    	JMeterTreeNode p1 = new JMeterTreeNode("me@this.com");
//        p1.addChild(to1);
//        p1.addChild(to2);
//        p1.addChild(to3);
//
//        JMeterTreeNode to4 = new JMeterTreeNode("Inbox");
//        JMeterTreeNode p2 = new JMeterTreeNode("other@aol.com");
//        p2.addChild(to4);
//
//        JMeterTreeNode root = new JMeterTreeNode("");
//        root.addChild(p1);
//        root.addChild(p2);
        return root;
    }

}
